/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.ConexionBD;
import java.sql.*;

import Modelo.DAL.Asistencia_DAO;
/**
 *
 * @author Jose Leonardo
 */
public class nombreEspaciosAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String sede = request.getParameter("sede");
        response.setContentType("text/json");
        JSONObject data = new JSONObject();
        Asistencia_DAO asd = new Asistencia_DAO();
        //data.put("NombreEspacios", consultarNombreEspacios());
        data.put("NombreEspacios", asd.consultarNombreEspacios(sede));
        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();
        
        return mapping.findForward(SUCCESS);
    }

    public JSONObject consultarNombreEspacios(){
        ConexionBD conex = new ConexionBD();
        JSONObject nombreEspacios = new JSONObject();
        try{
            Statement st = conex.session.createStatement();
            ResultSet rs = st.executeQuery("select nombre_espacio from espacio");
            int i = 0;
            while(rs.next()){
                JSONObject nEspacio = new JSONObject();
                nEspacio.put("nombre",rs.getString(1));
                nombreEspacios.put(""+i++,nEspacio);
            }
        } catch (Exception e){

        }
        return nombreEspacios;
    }
}
